'use strict';

const Service = require('egg').Service;

class GoodsService extends Service {
  //  获取分页列表的数据
  async list(pageNo = 0, pageSize = 6) {
    const { ctx } = this;
    const login_key_obj = await ctx.service.loginKey.getLoginKey();
    const result = await ctx.curl(`https://weapp-api.microsoftstore.com.cn/weapp/v5/productGroup/bskusurface/products?currentPage=${pageNo}&&pageSize=${pageSize}`, {
      headers: {
        'X-WX-Skey': login_key_obj.wx_skey,
        'X-WX-Id': login_key_obj.wx_id,
      },
      dataType: 'json',
    });
    if (result.data.code === -1) {
      await ctx.service.loginKey.refreshLoginKey();
      await ctx.service.loginKey.sendEmail('token过期通知', '请及时更换token');
    }
    return result.data;
  }
  // 查看某个商品是否可以买
  async searchGoodInStock(variantId = 'MIC2421') {
    const { ctx } = this;
    const login_key_obj = await ctx.service.loginKey.getLoginKey();
    const result = await ctx.curl(`https://weapp-api.microsoftstore.com.cn/weapp/v2/product/${variantId}/detail`, {
      headers: {
        'X-WX-Skey': login_key_obj.wx_skey,
        'X-WX-Id': login_key_obj.wx_id,
      },
      dataType: 'json',
    });
    if (result.data.code === -1) {
      await ctx.service.loginKey.refreshLoginKey();
      await ctx.service.loginKey.sendEmail('token过期通知', '请及时更换token');
    }
    return result.data;
  }
}

module.exports = GoodsService;
